package com.base.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.base.http.common.BaseHttpClient;
import com.base.http.common.BaseParams;
import com.base.http.entity.METHOD;
import com.base.http.listener.HttpCallback;

/**
 * frragment基础类
 * Created by Android on 2017/3/7 0007.
 */

public abstract class BaseFragment extends Fragment {

    protected BaseActivity mAct;
    public FragmentClick click;
    public HttpClick httpClick;
    public BaseParams mParams;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (mParams == null)
            mParams = new BaseParams();
        if (activity instanceof BaseActivity)
            mAct = (BaseActivity) activity;
        click = new FragmentClick();
        httpClick=new HttpClick();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return initView(inflater, container, savedInstanceState);
    }

    /**
     * 初始化布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    protected abstract View initView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState);

    /**
     * 初始化数据
     * @param savedInstanceState
     */
    protected abstract void initData(Bundle savedInstanceState);

    /**
     * 初始化监听
     */
    protected abstract void initListener();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(savedInstanceState);
        initListener();
    }

    /**
     * 替换fragment
     */
    public void replaceContent(int containerViewId, Fragment fragment) {
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();
        transaction.replace(containerViewId, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    /**
     * click事件
     * @param id
     */
    public void setOnClickListener(int id) {
        if (click == null){
            click = new FragmentClick();
        }
        mAct.findViewById(id).setOnClickListener(click);
    }

    /**
     * 重写click事件
     */
    public class FragmentClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            treatClickEvent(v.getId());
            treatClickEvent(v);
        }
    }

    public void treatClickEvent(int id){

    }

    public void treatClickEvent(View view){

    }

    /**
     * toast提示框
     * @param str
     */
    public void showToast(String str){
        Toast.makeText(mAct, str, Toast.LENGTH_SHORT).show();
    }
    public void showToast(int resId) {
        Toast.makeText(mAct, resId, Toast.LENGTH_SHORT).show();
    }


    /**
     * 初始化请求参数
     */
    protected void initParameter(){
        if (mParams == null){
            mParams = new BaseParams();
        } else {
            mParams.clear();
        }
    }

    /**
     * http相关
     */
    public class HttpClick extends HttpCallback {

        @Override
        public void success(String content, BaseHttpClient client, Object parse) {
            onSuccess(content, client, parse);
        }

        @Override
        public void error(Throwable error, BaseHttpClient client) {
            onError(error, client);
        }
    }


    /**
     * get请求
     * @param url
     * @param params
     * @param cls
     */
    public void sendGetRequest(String url, String urlType, BaseParams params, Class<?> cls){
        BaseHttpClient.newBuilder().url(url)
                .urlIdentifier(urlType)
                .setBaseParams(params)
                .setParse(cls)
                .method(METHOD.GET)
                .build().execute(httpClick);
    }

    /**
     * get加密请求
     * @param url
     * @param params
     * @param cls
     */
    public void sendEncryptGetRequest(String url, String urlType, BaseParams params, Class<?> cls) {
        BaseHttpClient.newBuilder().url(url)
                .urlIdentifier(urlType)
                .setBaseParams(params)
                .setParse(cls)
                .method(METHOD.GET)
                .setEncrypt(true)
                .build().execute(httpClick);
    }

    /**
     * post请求
     * @param url
     * @param params
     * @param cls
     */
    public void sendPostRequest(String url, String urlType, BaseParams params, Class<?> cls){
        BaseHttpClient.newBuilder().url(url)
                .urlIdentifier(urlType)
                .setBaseParams(params)
                .setParse(cls)
                .method(METHOD.POST_FORM)
                .build().execute(httpClick);
    }

    /**
     * post加密请求
     * @param url
     * @param params
     * @param cls
     */
    public void sendEncryptPostRequest(String url, String urlType, BaseParams params, Class<?> cls) {
        BaseHttpClient.newBuilder().url(url)
                .urlIdentifier(urlType)
                .setBaseParams(params)
                .setParse(cls)
                .method(METHOD.POST_FORM)
                .setEncrypt(true)
                .build().execute(httpClick);
    }

    /**
     * 成功返回
     * @param content
     * @param client
     * @param parse
     */
    protected abstract void onSuccess(String content, BaseHttpClient client, Object parse);

    /**
     * 失败返回
     */
    protected abstract void onError(Throwable error, BaseHttpClient client);
}
